package pervasivePositioning.androidPositioner;

import java.io.IOException;

import pervasivePositioning.pos.Coordinate;
import android.location.Location;
import android.os.AsyncTask;

public class TCPPositionService implements PositionService { //extends Application {
	
	//Ice.ObjectPrx obj;
	//PositionServicePrx service;
	
	//private Ice.Communicator _communicator;
	TCPClient tcp;
	String serverip;
	int serverport;
	private boolean enabled = true;
	
	public TCPPositionService(String server, int port) throws IOException
	{
		serverip = server;
		serverport = port;
		tcp = new TCPClient(serverip, serverport);
	}
	
	@Override
	public void ReportPosition(long time, Coordinate position, Coordinate acc, String configValue) throws IOException
	{
		//service.reportPosition(position, time, strategyName, configValue);
		//Thread tcpThread = new Thread(new TCPClient());
		//tcpThread.start();
		
		//tcp = new TCPClient(serverip, serverport);
		//SendFileImageTask task = new SendFileImageTask();
		if(!enabled)
			return;
		
		SendPositionTask task = new SendPositionTask();
		//task.onResult = callback;
		pervasivePositioning.androidPositioner.Report theReport = 
			new pervasivePositioning.androidPositioner.Report(position, time, "", configValue);
		//reports[0] = new pervasivePositioning.androidPositioner.Report(position, time, strategyName, configValue);
		task.execute(theReport);
	}
	
	//the asynchronous task to send a coordinate
	class SendPositionTask extends AsyncTask<pervasivePositioning.androidPositioner.Report,Void,Void> {

		@Override
		protected Void doInBackground(pervasivePositioning.androidPositioner.Report... params) {
			try {
				//tcp = new TCPClient(serverip, serverport); // done in the constructor now!
				tcp.sendReport(params[0]);
				return null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	}

	//reset, save and send are intentionally empty
	@Override
	public void reset() {}

	@Override
	public void save() {}
	
	@Override
	public void send() {}
	
	public boolean enable(boolean b) {
		boolean wasEnabled = b;
		enabled = b;
		return wasEnabled;
	}
	
	/*
	//@Override
	public int run(String[] args) {
	
		//this.obj = communicator().stringToProxy("position:tcp -h 192.168.1.2 -p 11000");
		//this.service = PositionServicePrxHelper.uncheckedCast(obj);
		//this.service.reportPosition(new Coordinate(), 0, "PeriodicStrategy", "configValue");
	    new Thread(new Runnable()
        {
            public void run()
            {
                initializeCommunicator();
            }

        }).start();
    return 0;
	}
	
	public void initializeCommunicator() throws IOException {
		
		// TODO Auto-generated method stub
		//_communicator = Ice.Util.initialize();
		//this.obj = _communicator.stringToProxy("position:tcp -h 192.168.1.2 -p 11000");
		//this.service = PositionServicePrxHelper.uncheckedCast(obj);
	}*/
}
